﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AudioCore.Entity;
using AudioCore.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace AudioCore.DAO
{
    internal class ArticlePartDao : BaseDao
    {
        internal ArticlePartDao()
            : base("ArticlePart", "ArticlePartID")
        {
            db = DatabaseFactory.CreateDatabase(Constant.DATABASE);
        }

        internal int GetLastSortOrder(int articleID)
        {
            string query = "select top 1 sortorder from articlepart where articleid = {0} order by sortorder desc";
            query = string.Format(query, articleID);
            object objSortOrder = db.ExecuteScalar(CommandType.Text, query);
            if (objSortOrder != null)
            {
                return Int32.Parse(objSortOrder.ToString());
            }
            else
            {
                return -1;
            }
        }

        internal int UpdateStatusArticlePart(int articlePartID, bool actived)
        {
            int status = actived ? 1 : 0;
            string query = "update ArticlePart set Actived = {0} where ArticlePartID = {1}";
            query = string.Format(query, status, articlePartID);
            return db.ExecuteNonQuery(CommandType.Text, query);
        }

        internal DataTable GetArticlePartByID(int accountID, int articlePartID)
        {
            object[] param = new object[] {accountID, articlePartID};
            DataSet dst = db.ExecuteDataSet("sp_AUDIO_GetArticlePartByID", param);
            return dst.Tables[0];
        }

        internal DataTable GetArticlePartByPage(int accountID, int articleID, int pageIndex, int pageSize, ref int counter)
        {
            object[] param = new object[] { accountID, articleID, (int)Constant.ItemType.ArticlePart, pageIndex, pageSize };
            DataSet dst = db.ExecuteDataSet("sp_AUDIO_GetArticlePartByPage", param);
            counter = int.Parse(dst.Tables[0].Rows[0][0].ToString());

            return dst.Tables[1];
        }
    }
}
